package com.zhuiyun.project.api.equipment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.caseRecord.entity.CaseRecord;
import com.zhuiyun.project.api.equipment.entity.Equipment;
import com.zhuiyun.project.api.equipment.model.EquipmentModel;
import com.zhuiyun.project.api.equipment.service.EquipmentService;
import com.zhuiyun.project.api.venuemanagement.entity.VenueManagement;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName equipmentController
 * @Description TODO 器材管理控制层
 * @Author lpr
 * @Date 2023/11/8 11:04
 **/
@RestController
@RequestMapping("equipment")
@Api(tags = "器材管理")
@Slf4j
public class EquipmentController extends BaseController {
    @Resource
    EquipmentService equipmentService;
    /**
     * 分页查询+模糊查询 器材管理
     * @Author lpr
     * @Description
     * @Date 2023/11/02 19:29
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param pageRequest
     * @param equipment
     */
    @PreAuthorize("@ss.hasPermi('equipment:ement:list')")
    @ApiOperation(value = "器材管理查询")
    @OptLog(modular = "器材管理", type = CommonConstants.SELECT, describe = "器材管理查询 ")
    @GetMapping("/listEquipment")
    public CommonResult list(PageRequest pageRequest, Equipment equipment) {
        IPage<Equipment> list = equipmentService.selectEquipment(pageRequest, equipment);
        return CommonResult.ok(list);
    }
    /**
     * 查询 器材管理
     * @Author lpr
     * @Description
     * @Date 2023/11/02 19:29
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param venueUniqueIdentifier
     */
    @PreAuthorize("@ss.hasPermi('equipment:ement:query')")
    @ApiOperation(value = "器材管理查询")
    @OptLog(modular = "器材管理", type = CommonConstants.SELECT, describe = "器材管理查询 ")
    @GetMapping("/listvenueManagementByVenueId")
    public CommonResult listEquipmentByVenueId(String venueUniqueIdentifier) {
        List<VenueManagement> list = equipmentService.listvenueManagementByVenueId(venueUniqueIdentifier);
        return CommonResult.ok(list);
    }
    /**
     * @Description 新增器材信息
     * @Author lpr
     * @Date 2023/11/08 15:52
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param files
     * @param equipmentModel
     */
    @PostMapping("/addEquipment")
    @PreAuthorize("@ss.hasPermi('equipment:ement:add')")
    @ApiOperation(value = "新增器材信息")
    @OptLog(modular = "器材管理", type = CommonConstants.ADD, describe = "新增器材信息")
    public CommonResult addEquipment(MultipartFile[] files, EquipmentModel equipmentModel) throws CommonException {
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(equipmentModel)) {
            Equipment equipment = new Equipment();
            // equipmentModel 对象拷贝成 equipment
            BeanUtils.copy(equipmentModel, equipment);
            equipment.setCreateTime(new Date());
            int i = equipmentService.addEquipment(files, equipment);
            if (i > 0) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            } else {
                return CommonResult.error(EmErrorCode.SAVE_ERROR);
            }
        } else {
            return CommonResult.error(EmErrorCode.SAVE_ERROR);
        }
    }
    /**
     * @Description 修改器材信息
     * @Author lpr
     * @Date 2023/11/08 19:52
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param files
     * @param equipmentModel
     */
    @PutMapping("editEquipement")
    @PreAuthorize("@ss.hasPermi('equipment:ement:edit')")
    @ApiOperation(value = "修改器材信息")
    @OptLog(modular = "器材管理", type = CommonConstants.UPDATE, describe = "修改器材信息")
    public CommonResult editEquipment(MultipartFile[] files, EquipmentModel equipmentModel) throws CommonException {
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(equipmentModel)) {
            Equipment equipment = new Equipment();
            // sysUserModel 对象拷贝成 sysUser
            BeanUtils.copy(equipmentModel, equipment);

            equipment.setScrapTime(new Date());
            return equipmentService.editEquipment(files, equipment);
        } else {
            return CommonResult.error(EmErrorCode.UPDATE_ERROR);
        }
    }
    /**
     * @Author lpr
     * @Description 状态修改
     * @Date 2023/11/08 19:40
     * @param equipment
     */
    @PreAuthorize("@ss.hasPermi('equipment:ement:edit')")
    @ApiOperation(value = "修改器材状态")
    @OptLog(modular = "器材管理", type = CommonConstants.UPDATE , describe = "修改器材状态")
    @PutMapping("/editEmentDisplay")
    public CommonResult editEmentDisplay(@RequestBody Equipment equipment){
        return CommonResult.ok(equipmentService.editEmentDisplay(equipment));
    }
    /**
     * @Author lpr
     * @Description 器材逻辑删除
     * @Date 2023/11/03 13:40
     * @param ementId
     */
    @PreAuthorize("@ss.hasPermi('equipment:ement:remove')")
    @ApiOperation(value = "器材逻辑删除")
    @OptLog(modular = "器材管理", type = CommonConstants.UPDATE , describe = "器材逻辑删除")
    @DeleteMapping("/removeEmentById/{ementId}")
    public CommonResult removeEmentById(@PathVariable Integer ementId){

        return equipmentService.removeEmentById(ementId);
    }

}
